package com.jinxiangyang.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jinxiangyang.annotation.OperateLog;
import com.jinxiangyang.domain.OperLog;
import com.jinxiangyang.service.OperLogService;
import com.jinxiangyang.utils.enums.BusinessType;
import com.jinxiangyang.utils.enums.OperatorType;
import com.jinxiangyang.utils.result.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author jinxiangyang
 * @date 2023/8/16 22:27
 * @description TODO
 **/

@RestController
@RequestMapping("/operlog")
@Api(tags = "操作日志接口")
public class OperLogController {



    @Autowired
    private OperLogService operLogService;






    @GetMapping("/{pageNumber}/{pageSize}")
    @ApiOperation(value = "分页查询日志")
    @OperateLog(title = "操作日志模块",operatorType = OperatorType.ADMIN, businessType = BusinessType.UPDATE)
    public AjaxResult getOperLogServicePage(@PathVariable Long pageNumber,
                                            @PathVariable Integer pageSize,
                                                           OperLog operLog){
        IPage<OperLog> operLogPage =  operLogService.getOperLogPage(pageNumber,pageSize,operLog);
        if( pageNumber > operLogPage.getPages()){
            operLogPage = operLogService.getOperLogPage(operLogPage.getPages(), pageSize,operLog);
        }
        return AjaxResult.success(operLogPage);
    }


    @DeleteMapping
    @ApiOperation(value = "清空操作日志表")
    @OperateLog(title = "操作日志模块",operatorType = OperatorType.ADMIN, businessType = BusinessType.DELETE)
    public AjaxResult deleteOperLog(){
      operLogService.cleanByOperLog();
      return AjaxResult.success();
    }
}
